﻿
using System;
using System.Collections.Generic;

namespace parser
{
	class Program
	{
		enum ParseState {PLAIN, NUM}
		
		public static Lexem[] Lexer(string s)
		{
			int l = s.Length;
			List<char> cur = new List<char>();
			ParseState ps = ParseState.PLAIN;
			List<Lexem> lx = new List<Lexem>();
			for(int i=0;i<l;i++)
			{
				char c = s[i];
				if(c==' ' || c=='\t' || c=='\n')
				{
					if(ps == ParseState.NUM)
					{
						string s1 = "";
						cur.ForEach( c1 => s1 += c1);
						cur.RemoveRange(0,cur.Count);
						lx.Add(new Lexem(LexemType.VALUE, s1));
						ps = ParseState.PLAIN;
					}
					continue;
				}
				
				if(c=='+' || c=='-' || c=='*' || c=='/')
				{
					if(ps == ParseState.NUM)
					{
						string s1 = "";
						cur.ForEach( c1 => s1 += c1);
						cur.RemoveRange(0,cur.Count);
						lx.Add(new Lexem(LexemType.VALUE, s1));
						ps = ParseState.PLAIN;
					}
		

					lx.Add(new Lexem(LexemType.OPER, c.ToString()));
					continue;
				}
				
				
				if(Char.IsNumber(c))
				{
					cur.Add(c);
					ps = ParseState.NUM;
					continue;
				
				}
				
					
					
					
					
			}
			if(ps == ParseState.NUM)
			{
				string s1 = "";
				cur.ForEach( c1 => s1 += c1);
				cur.RemoveRange(0,cur.Count);
				lx.Add(new Lexem(LexemType.VALUE, s1));
				ps = ParseState.PLAIN;
			}
					
			return lx.ToArray();
		}



	
		public static void Main(string[] args)
		{
			Console.Write("Enter string:");
			string s = Console.ReadLine();
			Lexem[] lexs = Lexer(s);
			TreeNode tr = Parser.Parse(lexs);
			
			Decimal res = Evaluator.Evaluate(tr);
			
			Console.WriteLine("Result: "+ res);
			
			
			
			
			
			// TODO: Implement Functionality Here
			
			Console.Write("Press any key to continue . . . ");
			Console.ReadKey(true);
		}
	}
}